package assignments.sagarjoshi.springtms;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class NeworEditTrainee extends Activity {

    EditText eId,eName,eJoiningDate,eClgName,eTechnology, eEmail;
    String sId,sName,sJoiningDate,sClgName,sTechnology,sEmail, validName;
    Button bSave, bReset;
    int val;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.neworeditrainee);

        ActionBar b = getActionBar();
        b.setTitle("New/Edit Trainee");
        b.show();

        Intent intent = getIntent();
        val=intent.getIntExtra("item_id",0);
        Log.i("value for intent", String.valueOf(val));

        eId = (EditText) findViewById(R.id.editText);
        eName = (EditText) findViewById(R.id.editText3);
        eJoiningDate = (EditText) findViewById(R.id.editText4);
        eClgName = (EditText) findViewById(R.id.editText5);
        eTechnology = (EditText) findViewById(R.id.editText6);
        eEmail = (EditText) findViewById(R.id.editText11);


        bSave = (Button) findViewById(R.id.button3);
        bReset = (Button) findViewById(R.id.button4);

        if (val>0){
            MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());
            Cursor cursor = myDatabaseHelper.EditTrainee(val);

            eId.setText(cursor.getString(1));
            eName.setText(cursor.getString(2));
            eEmail.setText(cursor.getString(3));
            eClgName.setText(cursor.getString(4));
            eTechnology.setText(cursor.getString(5));
            eJoiningDate.setText(cursor.getString(6));
        }

        final Context context = this;

        bSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sId = eId.getText().toString();
                sName = eName.getText().toString();
                sJoiningDate = eJoiningDate.getText().toString();
                sClgName = eClgName.getText().toString();
                sTechnology = eTechnology.getText().toString();
                sEmail = eEmail.getText().toString();
                ////////////////////////
                validName = sName.replaceAll("\\s+", " ").trim();
                ///////////////////////

                if(val==0){
                    if (sId.isEmpty() || validName.isEmpty() || sEmail.isEmpty() || sJoiningDate.isEmpty() || sClgName.isEmpty() || sTechnology.isEmpty()) {
                        Toast.makeText(getApplicationContext(), "Enter all fields...", Toast.LENGTH_LONG).show();
                    } else {
                        Trainee trainee = new Trainee(0,sId, validName, sEmail, sClgName, sTechnology, sJoiningDate);
                        MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());
                        int val = myDatabaseHelper.AddTrainee(NeworEditTrainee.this, trainee);
                        if (val == 0) {
                            eId.setText("");
                            eName.setText("");
                            eJoiningDate.setText("");
                            eClgName.setText("");
                            eTechnology.setText("");
                            eEmail.setText("");
                            ((Activity) context).finish();
                        }
                    }
                }
                else {
                    MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());
                    Trainee trainee = new Trainee(val, sId,validName,sEmail,sClgName,sTechnology,sJoiningDate);
                    int res=myDatabaseHelper.UpdateTrainee(trainee);
                    if(res>0){
                        Toast.makeText(getApplicationContext(),"Record updated successfully...",Toast.LENGTH_LONG).show();
                        ((Activity) context).finish();
                    }

                }

            }
        });

        bReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eId.setText("");
                eName.setText("");
                eJoiningDate.setText("");
                eClgName.setText("");
                eTechnology.setText("");
                eEmail.setText("");
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_newor_edit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
